package com.example.demospring.controller.counselor;


import com.example.demospring.entity.ConsultationCaseReport;
import com.example.demospring.service.counselor.ConsultationCaseReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/consultation-case-reports")
public class ConsultationCaseReportController {

    @Autowired
    private ConsultationCaseReportService reportService;

    // 新增/编辑
    @PostMapping
    public ConsultationCaseReport createOrUpdate(@RequestBody ConsultationCaseReport report) {
        if (report.getId() == null) {
            report.setCreatedAt(LocalDateTime.now());
        }
        return reportService.save(report);
    }

    // 查询详情（by id）
    @GetMapping("/{id}")
    public ConsultationCaseReport getById(@PathVariable Integer id) {
        Optional<ConsultationCaseReport> reportOpt = reportService.findById(id);
        return reportOpt.orElse(null);
    }

    @GetMapping("/by-appointment/{appointmentId}")
    public ResponseEntity<ConsultationCaseReport> getByAppointmentId(@PathVariable Integer appointmentId) {
        return reportService.findByAppointmentId(appointmentId)
                .map(report -> ResponseEntity.ok(report))
                .orElse(ResponseEntity.notFound().build());
    }
    // 删除
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        reportService.deleteById(id);
    }

    // 可扩展分页列表查询等
}
